package cn.net.shuxing.yifu.common.back.controller;

import cn.net.shuxing.yifu.common.bean.*;
import cn.net.shuxing.yifu.common.constant.CodeConst;
import cn.net.shuxing.yifu.common.constant.RegistType;
import cn.net.shuxing.yifu.common.constant.UserState;
import cn.net.shuxing.yifu.common.constant.UserType;
import cn.net.shuxing.yifu.common.dto.BaseDto;
import cn.net.shuxing.yifu.common.service.*;
import cn.net.shuxing.yifu.common.util.ExcelUtil;
import cn.net.shuxing.yifu.common.util.JedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;

@RestController
public class ExcelController {

    @Autowired
    protected UserInfoService userInfoService;

    @Autowired
    protected UserRealInfoService userRealInfoService;

    @Autowired
    protected UserService userService;

    @Autowired
    protected UserCapitalService userCapitalService;

    @Autowired
    protected UserBankCardService userBankCardService;

    @Autowired
    private CardTypeService cardTypeService;

    @Autowired
    protected ShareholderBonusService shareholderBonusService;

    @Autowired
    protected ShareholderLevelService shareholderLevelService;

    @Autowired
    protected ShareholderCoinGrantHistoryService shareholderCoinGrantHistoryService;

    @Autowired
    protected ShareholderShouldPayService shareholderShouldPayService;

    @Autowired
    protected LevelGrantService levelGrantService;

    @Autowired
    protected ShareholderWalletService shareholderWalletService;

    @Autowired
    private  CoinTypeService coinTypeService;

    @Autowired
    private UserInfoBindService userInfoBindService;

    @Autowired
    private ShareholderCommonService shareholderCommonService;

    @Autowired
    private ShareholderService shareholderService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    protected JedisUtil jedisUtil;

    @Value("user.picture.default")
    private String defaultPicturePath;

    @RequestMapping("/back/user/shareholder/uploadExcel")
    public BaseDto addShareholderByUploadExcel(@RequestParam("fileName") MultipartFile file, HttpServletRequest request)
    {
        BaseDto baseDto = new BaseDto();
        StringBuilder errorMsgBuilder = new StringBuilder();
        if(file.isEmpty()){
            baseDto.setCode(CodeConst.ERROR);
            baseDto.setMsg("文件为空");
            return baseDto;
        }

        try {
            ExcelUtil.deal(file.getInputStream(),(data)->{
//                股东姓名、手机号、证件类型、证件号码、开户银行、开户支行、银行卡号、休眠币数量、股东等级、登录密码
                if (data.size() == 11)
                {
                    String realName = data.get(0);
                    String phoneNum = data.get(1);
                    String cardTypeName = data.get(2);
                    String idCardNum = data.get(3);
                    String openBank = data.get(4);
                    String openBankBranch = data.get(5);
                    String bankCardNum = data.get(6);
                    String sleepCoinCount = data.get(7);
                    String shareholderLevel = data.get(8);
                    String password = data.get(9);
                    String coinTypeSimpleName = data.get(10);

                    CardType cardType = cardTypeService.getByCardTypeName(cardTypeName);
                    if (cardType == null)
                        return;
                    CoinType coinType = coinTypeService.getByCoinTypeSimpleName(coinTypeSimpleName);
                    if (coinType == null)
                        return;
                    shareholderService.registShareholder(realName,phoneNum,cardType.getId(),idCardNum,openBank,openBankBranch,bankCardNum,new BigDecimal(sleepCoinCount),Integer.valueOf(shareholderLevel),password,coinType.getId());
                }
                else
                {
                    baseDto.setCode(CodeConst.ERROR);
                    baseDto.setData("数据格式不对");
                }
            });
            return baseDto;
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (baseDto.getCode() == CodeConst.ERROR)
        {
            baseDto.setMsg(errorMsgBuilder.toString());
        }
        else
        {
            baseDto.setData("添加成功");
        }
        return baseDto;
    }


    /**
     * 批量导入兼职股东
     * @param file
     * @param request
     * @return
     */
    @RequestMapping(value = "/back/parttime/shareholder/uploadExcel",method = RequestMethod.POST)
    public BaseDto addParttimeShareholderByUploadExcel(@RequestParam("fileName") MultipartFile file, HttpServletRequest request)
    {
        BaseDto baseDto = new BaseDto();
        StringBuilder errorMsgBuilder = new StringBuilder();
        if(file.isEmpty()){
            baseDto.setCode(CodeConst.ERROR);
            baseDto.setMsg("文件为空");
            return baseDto;
        }

        try {
            ExcelUtil.deal(file.getInputStream(),(data)->{
//                股东姓名、手机号、开户银行、开户支行、银行卡号、登录密码
                if (data.size() == 6)
                {
                    String realName = data.get(0);
                    String phoneNum = data.get(1);
                    String openBank = data.get(2);
                    String openBankBranch = data.get(3);
                    String bankCardNum = data.get(4);
                    String password = data.get(5);
                    sysUserService.add(null,realName,null,phoneNum,openBank,openBankBranch,bankCardNum,password);
                }
                else
                {
                    baseDto.setCode(CodeConst.ERROR);
                    baseDto.setData("数据格式不对");
                }
            });
            return baseDto;
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (baseDto.getCode() == CodeConst.ERROR)
        {
            baseDto.setMsg(errorMsgBuilder.toString());
        }
        else
        {
            baseDto.setData("添加成功");
        }
        return baseDto;
    }
}
